package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.User;

public class UserDao {

	private static UserDao instance;
	private BaseDao baseDao;

	protected UserDao() {
		this.baseDao = BaseDao.getInstance();
	}

	public static UserDao getInstance() {
		if (instance == null) {
			instance = new UserDao();
		}
		return instance;
	}

	public List<User> getListUser(String condition) throws Exception {
		String query = "SELECT * FROM users";
		if(condition != null && !"".equals(condition)) {
			query = query + " " + condition;
		}
		ResultSet rs = baseDao.executeQuery(query);
		List<User> users = new ArrayList<User>();
		while (rs.next()) {
			User u = new User(rs.getString("USERNAME"),
					rs.getString("PASSWORD"), 
					rs.getString("DISPLAY_NAME"),
					rs.getString("ROLE"), 
					rs.getDate("CREATE_DATE"));
			users.add(u);
		}

		return users;
	}
	
	public User getUser(String userName)  throws Exception {
		List<User> users = getListUser(" WHERE username = '" + userName + "'");
		if(users.size() > 0) {
			return users.get(0);
		}
		return new User();
	}
}
